package com.app.utils;

import android.app.Activity;

import com.app.application.QXApplication;
import com.app.config.ApplicationConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import phone.qyg.com.qyg.R;


/**
 * 作者：lblbh on 2017/7/26 15:53
 * 邮箱：lanbuhan@163.com
 * 功能：分享通用接口
 */
public class ShareOtherUtils  {

    public static ShareAction shareTool(final Activity activity, final String url, final String title, final  String content, final String fixImgUrl){

        /**
         SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
         SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
         */

        ShareAction mShareAction = new ShareAction(activity).setDisplayList(
                SHARE_MEDIA.WEIXIN,
                SHARE_MEDIA.SINA)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        UMWeb web = new UMWeb(url);
                        web.setTitle(title);
                        String survey = content;
                        if(CommonUtil.isRequestStr(survey) && survey.length() > ApplicationConfig.SHARD_DETAILS){
                            survey = survey.substring(0,ApplicationConfig.SHARD_DETAILS);
                        }
                        web.setDescription(CommonUtil.setAttributeText(survey));
//
                        if (!share_media.equals(SHARE_MEDIA.SINA)) {
                            if (CommonUtil.isRequestStr(fixImgUrl))
                                web.setThumb(new UMImage(activity, fixImgUrl));
                            else
                                web.setThumb(new UMImage(activity, R.mipmap.ic_launcher));

                            new ShareAction(activity).withMedia(web)
                                    .setPlatform(share_media)
                                    .setCallback(new CustomShareListener(activity))
                                    .share();
                        } else {
                            UMImage umImage;
                            if (!CommonUtil.isRequestStr(fixImgUrl)) {
                                umImage = new UMImage(QXApplication.getContext(), R.mipmap.ic_launcher);
                            }else{
                                umImage = new UMImage(QXApplication.getContext(), fixImgUrl);
                            }

                            umImage.setThumb(new UMImage(activity, R.mipmap.ic_launcher));
                            umImage.setTitle(title);
                            new ShareAction(activity).withMedia(umImage).withText(survey + url).withSubject(title)
                                    .setPlatform(share_media)
                                    .setCallback(new CustomShareListener(activity))
                                    .share();
                        }

                    }
                });

        return mShareAction;
    }

    private static class CustomShareListener implements UMShareListener {


        private CustomShareListener(Activity activity) {
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (platform.name().equals("WEIXIN_FAVORITE")) {
//                Toast.makeText(mActivity.get(),"收藏成功", Toast.LENGTH_SHORT).show();
                PromptManager.showToast("收藏成功");
            } else {
                if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                        && platform != SHARE_MEDIA.EMAIL
                        && platform != SHARE_MEDIA.FLICKR
                        && platform != SHARE_MEDIA.FOURSQUARE
                        && platform != SHARE_MEDIA.TUMBLR
                        && platform != SHARE_MEDIA.POCKET
                        && platform != SHARE_MEDIA.PINTEREST

                        && platform != SHARE_MEDIA.INSTAGRAM
                        && platform != SHARE_MEDIA.GOOGLEPLUS
                        && platform != SHARE_MEDIA.YNOTE
                        && platform != SHARE_MEDIA.EVERNOTE) {
                    //platform +
//                    Toast.makeText(mActivity.get(), "分享成功", Toast.LENGTH_SHORT).show();
                    PromptManager.showToast("分享成功");
                }

            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST

                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
//                Toast.makeText(mActivity.get(), "分享失败啦", Toast.LENGTH_SHORT).show();
                PromptManager.showToast("分享失败啦");
                if (t != null) {
                   SystemPrintl.systemPrintl(t.getMessage());
                }
            }

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {

//            Toast.makeText(mActivity.get(), "分享取消了", Toast.LENGTH_SHORT).show();
        }
    }
}
